package com.kgo.eidtor.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.kgo.eidtor.dao.ModuleBeanMapper;
import com.kgo.eidtor.entity.ModuleBean;
import com.kgo.eidtor.entity.SysCodeBean;
import com.kgo.eidtor.service.ModuleService;
import com.kgo.eidtor.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ModuleServiceImpl implements ModuleService {
    @Resource
    private ModuleBeanMapper moduleBeanMapper;
    @Override
    public String getModuleCode() {
        List<SysCodeBean> moduleCodes = moduleBeanMapper.getModuleCode();
        int count = moduleCodes.size();
        return ResultUtil.query(count,moduleCodes);
    }

    @Override
    public String addModule(ModuleBean moduleBean) {
        if (StringUtils.isBlank(moduleBean.getId())){
            moduleBean.setId(IdUtil.simpleUUID());
        }
        if (StringUtils.isBlank(moduleBean.getName())){
            return ResultUtil.add(0,"新增模块失败，模块名称不能为空");
        }
        int addCount = moduleBeanMapper.insert(moduleBean);
        if (addCount<=0){
            return ResultUtil.add(0,"新增失败！未知错误，请联系管理员！");
        }
        return  ResultUtil.add(0,"新增成功",moduleBean.getId());
    }

    @Override
    public String getModules(ModuleBean moduleBean) {
        int moduleCount = moduleBeanMapper.getModuleCount(moduleBean);
        if (moduleCount<=0){
            return ResultUtil.query(0,null,"查询失败，无模块数据");
        }
        List<ModuleBean> moduleBeanList =  moduleBeanMapper.getModules(moduleBean);

        return ResultUtil.query(moduleCount,moduleBeanList,"查询项目模块成功！");
    }

    @Override
    public String getModuleById(String id) {
        if (StringUtils.isBlank(id)){
            return  ResultUtil.query(0,null,"获取模块失败！模块ID不能为空");
        }
        ModuleBean thismodule = moduleBeanMapper.selectByPrimaryKey(id);
        if (ObjectUtil.isNull(thismodule)){
            return ResultUtil.query(0,null,"获取模块信息失败！未知错误，请联系管理员");
        }
        return  ResultUtil.query(1,thismodule,"获取模块信息成功");
    }
}
